# _*_ coding: UTF-8 _*_

from django import template
from ..models import *
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.filter(name="bits_and")
def bits_and(value, arg):
    if int(value) & int(arg):
        return True

    return False


@register.filter(name="filename")
@stringfilter
def filename(value):
    return value.split("/")[-1]


@register.filter(name="download_url")
@stringfilter
def download_url(value):
    return "/static/uploaded/"+filename(value)


@register.filter(name="model_attr")
def items_to_list(model):
    its = []
    for i in model.items.all():
        its.append((i.key, i.value, i.title))

    return its
